package com.dididoctor.doctor.Activity.Usercentre.MyPurse.ChoicePayWay;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.dididoctor.doctor.Activity.Usercentre.MyPurse.CollectionMethods.BankBean;
import com.dididoctor.doctor.Activity.Usercentre.MyPurse.WithdrawList.WithdrawalsFailActivity;
import com.dididoctor.doctor.Activity.Usercentre.MyPurse.WithdrawList.WithdrawalsSuccedActivity;
import com.dididoctor.doctor.Dialog.ChoiceConfirmDialog;
import com.dididoctor.doctor.MV.EduActivity;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Utils.Util;

import java.util.List;

/**
 * 张强
 * Created by Administrator on 2016/4/19.
 */
public class PayWayActivity extends EduActivity implements PayWayView {
    private TextView mTvTitle;
    private ListView mlvBank;
    private PayWayAdapter adapter;
    private PayWayPresenter presenter;
    private List<BankBean> data;
    private Button mBtnDelete;
    private BankBean bean = null;
    private String balance = "";
    public static final int FAIL = 10001;
    public static final int SUCCESS = 10002;

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_payway);
    }

    @Override
    public void init() {

        /**头部**/
        initTitleView();

        /** 界面**/
        initview();

    }

    private void initview() {
        balance = getIntent().getStringExtra("balance");
        mlvBank = (ListView) findViewById(R.id.lv_bank);
        mBtnDelete = (Button) findViewById(R.id.btn_confirm);
        mBtnDelete.setOnClickListener(this);

        presenter = new PayWayPresenter(PayWayActivity.this, this);
        presenter.lunch();
    }

    private void initTitleView() {
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvTitle.setText("选择收款方式");
    }

    @Override
    public void onClickEvent(View view) {
        switch (view.getId()) {

            case R.id.btn_confirm:

                if (bean != null && !Util.isEmpty(balance)) {
                    ChoiceConfirmDialog dialog = new ChoiceConfirmDialog(PayWayActivity.this, bean, balance);
                    dialog.show();
                } else {
                    showToastMessage("请选择支付方式");
                }
                break;
        }
    }

    @Override
    public void getdoctorfail() {
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void updateData(int position) {
        if (data != null) {
            for (int i = 0; i < data.size(); i++) {
                if (position == i) {
                    data.get(i).setIsChoice(true);
                } else {
                    data.get(i).setIsChoice(false);
                }
            }
            bean = data.get(position);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void Withdrawals(BankBean bean, String num) {
        if (bean != null && !Util.isEmpty(num)) {
            presenter.outlayInsert(bean.getOutlayId(), num);
        }
    }

    @Override
    public void outLayInsert(String content) {
        if (content.equals("1")) {
            Intent intent = new Intent(PayWayActivity.this, WithdrawalsSuccedActivity.class);
            intent.putExtra("balance", balance);
            intent.putExtra("from", "0");
            startActivityForResult(intent, SUCCESS);
        } else {
            Intent intent = new Intent(PayWayActivity.this, WithdrawalsFailActivity.class);
            intent.putExtra("from", "0");
            startActivityForResult(intent, FAIL);
        }

    }

    @Override
    public void getdoctorsucced(List<BankBean> banks) {

        data = banks;
        adapter = new PayWayAdapter(PayWayActivity.this, data);
        mlvBank.setAdapter(adapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        PayWayActivity.this.finish();
        super.onActivityResult(requestCode, resultCode, data);
    }
}
